package com.instalinks.android.smspostservice.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.instalinks.android.smspostservice.AppTabBaseActivity;
import com.instalinks.android.smspostservice.R;
import com.instalinks.android.smspostservice.R.id;
import com.instalinks.android.smspostservice.utils.enums.ConfigParams;

public class MessageComposeActivity extends AppTabBaseActivity {

	private TextView lblSendAs;
	private EditText txtSendAs;
	private EditText txtSendTo;
	private EditText txtMessage;
	private ImageButton btnSend;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final Intent intent = getIntent();
		final String action = intent.getAction();

		if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
			setupMessageComposeShortcut();

			if (!mAppCfg.isServiceRunning()) {
				this.appNotifications.notifyClearAll();
			}
			this.finish();
			return;
		} else {
			this.initialize();
		}
	}

	private void setupMessageComposeShortcut() {
		Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
		String shortcutName = "";
		Parcelable iconResource;

		shortcutIntent.setClassName(this, this.getClass().getName());
		shortcutIntent.putExtra(this.getClass().getName(),
				"Thin Server Provided This Shortcut");
		shortcutName = "Message Compose";
		iconResource = Intent.ShortcutIconResource.fromContext(this,
				R.drawable.compose);

		// Then, set up the container intent (the response to the caller)

		Intent intent = new Intent();
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

		// Now, return the result to the launcher

		if(this.mAppCfg.isAdminConfigVisibleFlag())
			setResult(RESULT_OK, intent);
		else
			setResult(RESULT_CANCELED, intent);
	}

	private void initialize() {
		if(!this.mAppCfg.isAdminConfigVisibleFlag()){
			
			Intent i = new Intent();
			i.setClassName(getApplicationContext().getPackageName(),
					HomeActivity.class.getName());

			startActivity(i);
			this.finish();

			return;
		}
		
		
		this.setContentView(R.layout.message_compose);

		lblSendAs = (TextView) findViewById(id.compose_lblSendAs);
		txtSendAs = (EditText) findViewById(id.compose_txtSendAs);
		txtSendTo = (EditText) findViewById(id.compose_txtSendTo);
		txtMessage = (EditText) findViewById(id.compose_txtMessage);
		btnSend = (ImageButton) findViewById(id.compose_btnSend);

		if (!mAppCfg.isAdminConfigVisibleFlag()) {
			lblSendAs.setVisibility(View.GONE);
			txtSendAs.setVisibility(View.GONE);
		}

		btnSend.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				send_onclick();
			}
		});
	}

	private void send_onclick() {
		new SubTask("Sending message...").execute();
	}

	private void clearValues() {
		this.txtSendAs.setText("");
		this.txtSendTo.setText("");
		this.txtMessage.setText("");
	}

	@Override
	protected boolean doBackgroundWork() {
		return mAppCfg.sendDataMessage(ConfigParams.modes.flash, txtSendAs
				.getText().toString(), txtSendTo.getText().toString(),
				txtMessage.getText().toString());
	}

	@Override
	protected void doOnSuccess() {
		mAppCfg.alert("Message has been submitted");
		this.clearValues();
	}

	@Override
	protected void doOnFailure() {
		mAppCfg.alert("Error sending message. Try again");
	}

}
